package com.artqiyi.dollmachine.presenter;

import com.artqiyi.artqiyisdk.common.constant.ArtqiyiConstant;
import com.artqiyi.dollmachine.common.constant.Constant;
import com.artqiyi.dollmachine.common.constant.ServerConstant;
import com.artqiyi.dollmachine.presenter.base.BasePresenter;
import com.artqiyi.dollmachine.presenter.base.PresenterCallbackCollection;
import com.artqiyi.dollmachine.utils.Callback.MyStringCallback;
import com.artqiyi.dollmachine.utils.JSONUtils;
import com.artqiyi.dollmachine.utils.manager.HttpManager;
import com.artqiyi.dollmachine.utils.respone.CoinBuyRespone;
import com.artqiyi.dollmachine.utils.respone.DollDetailRespone;
import com.artqiyi.dollmachine.utils.respone.GameResultRespone;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import timber.log.Timber;


/**
 * Created by Administrator on 2017/10/31 0031.
 */

public class DollOperatePresenter extends BasePresenter {

    public void doOperateDone(int id) {
        doOperateToServer(id, ArtqiyiConstant.ACTION_DONE);
    }

    public void doOperateToServer(int id, int action) {
        if (id == 0) {
            return;
        }
        Map<String, String> params = new HashMap<>();
        params.put("machine_id", String.valueOf(id));
        params.put("action", String.valueOf(action));

        HttpManager.getHttpRequest(ServerConstant.dollOperate(), params, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onOperateFail(e.getMessage(), action);
            }

            @Override
            public void onResponse(String response, int id) {
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onOperateSuccess(action);
            }
        });
    }

    public void getDollDetail(int id) {
        HttpManager.getHttpRequest(ServerConstant.getDollDetail() + id, null, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onGetDollDetailFail(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                DollDetailRespone dollDetailRespone = JSONUtils.jsonString2Bean(response, DollDetailRespone.class);
                if (dollDetailRespone != null) {
                    ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onGetDollDetailSuccess(dollDetailRespone.getData());
                }
            }
        });
    }

    public void startDollGame(int id) {
        HashMap<String, String> params = new HashMap<>();
        params.put("machine_id", String.valueOf(id));
        HttpManager.postHttpRequest(ServerConstant.startDollGame(), params, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onStartDollGameFail(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                GameResultRespone gameResultRespone = JSONUtils.jsonString2Bean(response, GameResultRespone.class);
                if (null != gameResultRespone) {
                    ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onStartDollGameSuccess(gameResultRespone);
                }
            }
        });
    }

    public void getCoinBuy() {
        HttpManager.getHttpRequest(ServerConstant.getCoinBuy(), null, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onCoinBuyFail(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                CoinBuyRespone coinBuyRespone = JSONUtils.jsonString2Bean(response, CoinBuyRespone.class);
                ((PresenterCallbackCollection.DollOperateActivityCallBack) mCallback).onCoinBuySuccess(coinBuyRespone.getData());
            }
        });
    }

    public void enterOrExitRoom(int id, String actionType) {
        Timber.d("url:" + ServerConstant.enterOrExitRoom() + id + "/room");
        HashMap<String, String> params = new HashMap<>();
        params.put("action", actionType);
        HttpManager.postHttpRequest(ServerConstant.enterOrExitRoom() + id + "/room", params, new MyStringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Timber.d(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
//                Timber.d("加入房间成功");
            }
        });
    }

}
